package com.ft.core.utils.json;

import com.alibaba.fastjson.parser.DefaultJSONParser;
import com.alibaba.fastjson.parser.JSONLexer;
import com.alibaba.fastjson.parser.deserializer.ObjectDeserializer;
import com.ft.core.datatype.TimeSpan;

import java.lang.reflect.Type;

/**
 * @copyright (C), 2015-2019, 梵途科技有限公司
 * @fileName: TimeSpanDeserializer
 * @author: 李阳
 * @date: 2019/4/17 11:19
 * @description: 时间间隔反序列化
 */
public class TimeSpanDeserializer implements ObjectDeserializer {
    @SuppressWarnings("unchecked")
    @Override
    public <T> T deserialze(DefaultJSONParser parser, Type type, Object fieldName) {
        JSONLexer lexer = parser.getLexer();
        if (lexer.isBlankInput()) {
            return null;
        }
        return (T) TimeSpan.parse(lexer.stringVal());
    }

    @Override
    public int getFastMatchToken() {
        return 0;
    }
}